<template>
  <div class="container">
    <img
      class="logo"
      src="../../images/logo_lg.png"
      mode="aspectFit"
    >
    <button
      class="btn"
      open-type="getUserInfo"
      @getuserinfo="authLogin"
    >进入</button>
    <img
      class="bg"
      src="../../images/login_bg_1.png"
      mode="widthFix"
    >
  </div>
</template>

<script>
import { request, wxLogin, judgeUser } from "@/utils";
export default {
  data() {
    return {};
  },
  methods: {
    forBeauty() {
      var _this = this;
      wx.getSystemInfo({
        success(res) {
          // model中包含着设备信息
          var model = res.model;
          if (model.search("iPhone X") !== -1) {
            _this.globalData.isIpx = true;
          } else {
            _this.globalData.isIpx = false;
          }
          console.log(res.model);
        }
      });
      if (typeof wx.getMenuButtonBoundingClientRect === "function") {
        var obj = wx.getMenuButtonBoundingClientRect();
        if (!this.globalData.height) {
          if (obj.top) {
            this.globalData.height = obj.bottom + 8 < 60 ? 65 : obj.bottom + 8;
          } else if (obj.bottom) {
            this.globalData.height = obj.bottom < 60 ? 65 : obj.bottom;
          }
        }
      }
    },
    async authLogin(e) {
      var userInfo = e.mp.detail.userInfo;
      if (userInfo) {
        var adminUserId = this.$root.$mp.query.admin_user_id || 0;
        var data = {
          avatar_url: userInfo.avatarUrl,
          city: userInfo.city,
          country: userInfo.country,
          gender: userInfo.gender,
          language: userInfo.language,
          nickname: userInfo.nickName,
          province: userInfo.province,
          admin_user_id: adminUserId
        };
        this.userLogin(data)
        // 发起网络请求
      }
    },
    async userLogin(loginData) {
      const CODE = await wxLogin();
      var result = await request(
        "/v1/oauth/login",
        "get",
        {
          token: "asd123",
          code: CODE
        },
        "showLoading"
      );
      const TOKEN = result.data.token;
      var header = { Authorization: "Bearer " + TOKEN };
      await request("/v1/oauth/user", "post", loginData, "hideLoading", header);
      if (result.data.success) {
        wx.setStorageSync("token", TOKEN);
        if (this.$root.$mp.query.productId) {
          wx.navigateTo({
            url: "/pages/detail/main?id=" + this.$root.$mp.query.productId
          });
        } else {
          wx.switchTab({
            url: "/pages/index/main"
          });
        }
      }
    }
  },
  created() {
    this.forBeauty();
  },
  async mounted() {
    if (!wx.getStorageSync("token")) {
      return;
    } else if (this.$root.$mp.query.admin_user_id) {
      wx.removeStorageSync("token");
      return;
    } else if (this.$root.$mp.query.productId) {
      wx.navigateTo({
        url: "/pages/detail/main?id=" + this.$root.$mp.query.productId
      });
    } else {
      wx.switchTab({
        url: "/pages/index/main"
      });
    }
  }
};
</script>

<style>
page {
  height: 100%;
}
</style>

<style scoped lang="scss">
.container {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  align-items: center;
  button {
    border: none;
    background-color: transparent;
    box-sizing: border-box;
  }
  button::after {
    border: none;
  }
  height: 100%;
  .logo {
    margin-top: 380rpx;
    height: 120rpx;
  }
  .btn {
    margin-top: 100rpx;
    width: 230rpx;
    height: 70rpx;
    line-height: 68rpx;
    border-radius: 35rpx;
    color: #d0b47f;
    border: 2rpx solid #d0b47f;
    font-size: 32rpx;
    padding: 0;
  }
  .bg {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 750rpx;
  }
}
</style>




